//
//  LTQTabBar.swift
//  WeiBoProject
//
//  Created by MAC on 16/2/25.
//  Copyright © 2016年 LTQ. All rights reserved.
//

import UIKit

class LTQTabBar: UITabBar {

    //自定义视图三部曲
    //第一步: 重写 init
    override init(frame: CGRect) {
        
        //来添加视图和设置约束
        super.init(frame:frame)
        
        setupUI()
    }
    
    //自定义了一个视图 会走init(frame: CGRect)
    //系统怕你采用storyboard ,会给你一个提示
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//第二步:创建一个私有方法,来添加视图和设置约束
    private func setupUI(){
//        将创建的的视图添加到父控件中
        addSubview(composeButton)
        
    }
    
    
    
//    创建+Button
    
    lazy var composeButton:UIButton = {
    
        var btn = UIButton(type:UIButtonType.Custom)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: "clickBtn", forControlEvents: UIControlEvents.TouchUpInside)
        //设置大小
        btn.sizeToFit()
        
        return btn
    }()
    
    
    func clickBtn (){
        print("我被点击了")
    }
    
//    重新布局子控件的方法
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        
//        设置索引
        var index = 0
        
        let width = bounds.width/5
        let height = bounds.height
        
//        遍历UItabbar的内部控件
        
        for subview in subviews{
            
//          如果类是UITabBarButton 那么拿到进行修改
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                if index == 2{ index++ }
                
//            **注意一下标和宽相乘时 是Int 类型 和 Float 类型相乘
                subview.frame = CGRectMake(CGFloat(index) * width, subview.frame.origin.y, width, height)
                index++
                
            }

        }
        composeButton.frame = CGRectMake(2 * width, 0, width, height)
    
    }
    
    
    
}
